import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/alarm/all_alarm_records_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/alarm/get_alarm_type_entity.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class AlarmManagementController extends GetxController with GetSingleTickerProviderStateMixin {

  /// 列表未处置key
  final GlobalKey<CustomEasyRefreshListState> noRefreshIndicatorKey = GlobalKey<CustomEasyRefreshListState>();

  /// 列表已处置key
  final GlobalKey<CustomEasyRefreshListState> yesRefreshIndicatorKey = GlobalKey<CustomEasyRefreshListState>();

  // 侧边栏scaffoldKey
  final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();

  late  TabController tabController;

  // 输入的设备编码
  String? deviceCode;

  // 报警登记 1: 一级 2: 二级 3: 三级
  String? alarmLevel;

  // 报警等级
  List<String> alarmLevelList = ['一级','二级','三级'];

  /// 报警类型列表
  List<GetAlarmTypeEntity> alarmTypeList = [];

  // 报警类型
  String? alarmType;


  // 报警开始时间
  String? alarmStartTime;
  // 报警结束时间
  String? alarmEndTime;

  // 刷新 1: 已处置 0: 未处置
  void refreshData() {
    if(tabController.index == 1) {
      yesRefreshIndicatorKey.currentState?.onRefresh();
    } else {
      noRefreshIndicatorKey.currentState?.onRefresh();
    }
  }

  // 获取报警列表
  Future<List<AllAlarmRecordsEntity>> getAlarmList(int pageNum,int pageSize, {String? search,int? disposalStatus }) async {
    final List<AllAlarmRecordsEntity> list = [];
    // getAlarmRecordPage userPageList
    final res = await HttpUtil().generateGetRequest<List<AllAlarmRecordsEntity>>(Interface.getAlarmRecordPage,queryParameters: {
      'pageNum': pageNum,
      'pageSize': pageSize,
      'alarmType': alarmType, // 报警类型
      'startTime': alarmStartTime, // 报警开始时间
      'endTime': alarmEndTime, // 报警结束时间
      'disposalStatus': disposalStatus, // 处置状态 0: 未处置 1: 已处置
      'alarmLevel': alarmLevel, // 报警等级
    }).execute();

    res.onSuccess((result) {
      if(result.data != null ) {
        list.addAll(result.data!);
      }
    });
    return list;

  }

  // 获取报警类型
  Future<List<GetAlarmTypeEntity>> getAlarmType() async {
    final res = await HttpUtil().generateGetRequest<List<GetAlarmTypeEntity>>(Interface.getAlarmType).execute();
    res.onSuccess((result) {
      if(result.data != null ) {
        alarmTypeList = result.data!;
      }
    });
    return alarmTypeList;
  }

  // 重置
  void reset() {
    deviceCode = null;
    alarmLevel = null;
    alarmType = null;
    alarmStartTime = null;
    alarmEndTime = null;
    update();
  }

  // 确定
  void confirm() {
    refreshData();
    Get.back();
  }

  // 跳转未处置
  void toNotDispose(AllAlarmRecordsEntity item) {
    Get.toNamed(AppRoutes.undisposedDetail,arguments: item);
  }

  // 跳转已处置
  void toDisposed(AllAlarmRecordsEntity item) {
    Get.toNamed(AppRoutes.disposedDetail,arguments: item);
  }

  @override
  void onInit() {
    super.onInit();
    tabController = TabController(length: 2, vsync: this);
  }

  @override
  void onReady() {
    super.onReady();
    getAlarmType();
  }

  @override
  void onClose() {
    tabController.dispose();
    super.onClose();
  }

}
